﻿#ifndef _OI_LT360BASE_CHART_HPP_
#define _OI_LT360BASE_CHART_HPP_

#include "OiLT360Base.hpp"

#include "OiDataStructure.hpp"
#include "OiTool.hpp"

#define OI_BASE_SERIES_TYPE 0
#define OI_PROFILE_SERIES_TYPE           (OI_BASE_SERIES_TYPE + 1)
#define OI_DESIGN_SERIES_TYPE            (OI_BASE_SERIES_TYPE + 2)
#define OI_TOOL_SERIES_TYPE              (OI_BASE_SERIES_TYPE + 4)
#define OI_ALL_TYPE                      (OI_PROFILE_SERIES_TYPE | OI_DESIGN_SERIES_TYPE | OI_TOOL_SERIES_TYPE)

namespace Oi
{
    namespace Appl
    {
        class OI_LT360BASE_API ToolSeries : public QtCharts::QLineSeries
        {
        public:
            ToolSeries(QObject* parent = Q_NULLPTR) : QLineSeries(parent) {};
            ~ToolSeries() {};

            QtCharts::QAbstractSeries::SeriesType type() const
            {
                return (QtCharts::QAbstractSeries::SeriesType)(OI_TOOL_SERIES_TYPE);
            }
        };

        class OI_LT360BASE_API ProfileSeries : public QtCharts::QLineSeries
        {
        public:
            ProfileSeries(QObject* parent = Q_NULLPTR) : QLineSeries(parent) {};
            ~ProfileSeries() {};

            QtCharts::QAbstractSeries::SeriesType type() const
            {
                return (QtCharts::QAbstractSeries::SeriesType)(OI_PROFILE_SERIES_TYPE);
            }

        };

        class OI_LT360BASE_API DesignSeries : public QtCharts::QLineSeries
        {
        public:
            DesignSeries(QObject* parent = Q_NULLPTR) : QLineSeries(parent) {};
            ~DesignSeries() {};

            QtCharts::QAbstractSeries::SeriesType type() const
            {
                return (QtCharts::QAbstractSeries::SeriesType)(OI_DESIGN_SERIES_TYPE);
            }
        };

        class OI_LT360BASE_API Chart : public QtCharts::QChart
        {
            Q_OBJECT
        public:
            Chart(QGraphicsItem *parent = Q_NULLPTR, Qt::WindowFlags wFlags = Qt::WindowFlags());
            virtual ~Chart();

        public:
            QList<ProfileSeries*> updateProfile(const LTBase::Contours& lstPoint);
            QList<DesignSeries*> updateDesign(const LTBase::Contours& lstPoint);
            DesignSeries* updateDesign(const LTBase::PointCloud& lstPoint);
            void removeTools();
            void clearAllSeries();
            void getContourPoints(QList<QPointF>& points, int type);
            void getContours(QList<QList<QPointF>>& points, int type);
            void autoRange(double percentage = 0.8, int type = OI_ALL_TYPE, QPointF center = QPointF());
            void showSnapShot(LTBase::SnapShot snapshot, QColor profileColor = Qt::cyan, int profileWidth = 5, QColor designColor = Qt::red, int designWidth = 1, double percentage = 0.4, int type = OI_ALL_TYPE);
            void measurementSnapShot(LTBase::MeasurementSnapShot snapshot, QColor profileColor = Qt::cyan, int profileWidth = 5, QColor designColor = Qt::red, int designWidth = 1, double percentage = 0.4, int type = OI_ALL_TYPE);
            void showTemplate(LTBase::SnapShot snapshot, QColor designColor = Qt::red, int designWidth = 2);
            void alignTools();
            void updateTool(LTBase::AlgoOutput &output);
            void updateDataViewer(LTBase::AlgoOutput& output);

            void setXRange(float min, float max);

            void saveSetting(const QString& iniFilePath);
            void loadSetting(const QString& iniFilePath);

        public:
            void rigidTransform(cv::Mat transformation, QPointF transformOrigin, int type = OI_ALL_TYPE);
            void rigidTransform(double angle, double tx, double ty, int type = OI_ALL_TYPE, QPointF transformOrigin = QPointF(0,0));

        public:
            void setFontSize(int);
            void setDisplayModeSimple(bool);
            void setConnectionThreshold(double);
            void setLinkingThreshold(double);
            void setNoiseThreshold(double);
            void setDataViewerInterval(int);

        public slots:
            void onSettingChanged();

        signals:
            void settingChanged();

        protected:
            void showEvent(QShowEvent* event);
            void hideEvent(QHideEvent* event);
            bool event(QEvent* event);

        private:
            OI_DISABLE_COPY(Chart);
            OI_DECLARE_IMPL(Chart);
        };

    }
}

#endif